package tt.dz.appserver.mvc.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.service.avro.client.oms.dzttOmsServiceProtocol;
import tt.dz.appserver.util.CodeUtils;
import tt.dz.appserver.util.Constants;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.dzcode.OmsCode;
import tt.dz.appserver.util.enums.Response;


/**
 * 七牛云
 * @author 谢龙飞
 *
 */
@RestController
@RequestMapping("/qiniu")
public class QiniuController {

	@Resource(name="omsMessageProtocolRequestor")
	private dzttOmsServiceProtocol omsService;

	/**
	 * 获取七牛云token
	 * @return
	 */
	@RequestMapping(value="/getQiniuyunUpToken",produces={"application/json;charset=utf-8"},method=RequestMethod.GET)
	public String getQiniuyunUpToken(){
		
		ResultMappingEntity mappingEntity = null;
		try {
			
			//获取Token
			String resultJsonString = omsService.getQiniuyunUpToken(null).toString();
			
			//数据转换
			JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

			//获取code
			String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
			
			
			if(OmsCode.SUCCESS.equals(code)){
				String token = omsResultObject.getString("pojo");
				mappingEntity = Result.responseBody(Response.SUCCESS.getCode(), Response.SUCCESS.getMsg(), token);
			}
			else{
				Response response = CodeUtils.getCodeByOMSServerCode(code);
				mappingEntity = Result.responseBody(response.getCode(), response.getMsg());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			mappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return mappingEntity.toString();
	}
}
